﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb
{
    public partial class login : System.Web.UI.Page
    {
        Maticsoft.BLL.UserAdmin userbll = new Maticsoft.BLL.UserAdmin();
        Maticsoft.BLL.UserRoleRelation urr = new Maticsoft.BLL.UserRoleRelation();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            List<Maticsoft.Model.UserAdmin> userlist = userbll.GetModelList(string.Format("loginname='{0}'", txtuser.Text));
            if (userlist.Count < 1)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('该用户不存在!')", true);
                return;
            }
            else
            {
                if (userlist[0].password.Equals(txtpwd.Text))
                {

                    if (Session["CheckCode"] != null)
                    {
                        string checkcode = Session["CheckCode"].ToString();
                        if (this.code.Value == checkcode)
                        {
                            HttpCookie cookie = new HttpCookie("UserLogin");
                            cookie.Values.Add("ID", userlist[0].id.ToString());
                            cookie.Values.Add("Code", userlist[0].username.ToString());
                            cookie.Values.Add("Name", urr.GetModel(string.Format("userid='{0}'", userlist[0].id)).Rolename);
                            if (online.Checked)
                                cookie.Expires = DateTime.Now.AddDays(365);
                            else
                                cookie.Expires = DateTime.Now.AddDays(1);
                            Response.AppendCookie(cookie);
                            Server.Transfer("index.aspx");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误!')", true);
                            return;
                        }
                    }
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('密码错误!')", true);
                }
            }
        }
    }
}